using UnityEngine;
using UnityEngine.UI;

namespace Brick
{
	public class LineManager : MonoBehaviour
	{
		public Transform[] balls;

		public Transform smallBall;

		public Transform bigBall;

		public Transform bottompos;

		public Transform toppos;

		private int ballnums = 60;

		private int balldirect = 25;

		private bool isInit;

		private Transform bigBallGO;

		public Image bigBallImg;

		public Sprite[] ballsSp;

		public void changeBigBallBg()
		{
			if (bigBallImg != null)
			{
				bigBallImg.sprite = ballsSp[GameData.useBallIndex];
			}
		}

		public void OnCreateBalls(Vector2 formPos)
		{
			changeBigBallBg();
			if (!isInit)
			{
				isInit = true;
				bigBallGO = UnityEngine.Object.Instantiate(bigBall);
				bigBallImg = bigBallGO.GetComponent<Image>();
				balls = new Transform[ballnums];
				for (int i = 0; i < ballnums; i++)
				{
					Transform transform = UnityEngine.Object.Instantiate(smallBall);
					transform.SetParent(base.transform);
					transform.localScale = Vector3.one;
					transform.localPosition = formPos;
					transform.gameObject.layer = 8;
					transform.gameObject.SetActive(value: false);
					balls[i] = transform;
				}
				bigBallGO.SetParent(base.transform);
				bigBallGO.gameObject.SetActive(value: false);
				changeBigBallBg();
			}
		}

		public void Change(Vector2 direction, Vector2 formPos)
		{
			bool flag = false;
			bigBallGO.gameObject.SetActive(value: false);
			Vector2 vector = direction;
			Vector2 v = formPos;
			int num = 0;
			for (int i = 0; i < balls.Length; i++)
			{
				if (flag)
				{
					balls[i].localPosition = new Vector2(v.x + vector.x * (float)balldirect * (float)(i - num), v.y + vector.y * (float)balldirect * (float)(i - num));
				}
				else
				{
					balls[i].localPosition = new Vector2(formPos.x + direction.x * (float)balldirect * (float)i, formPos.y + direction.y * (float)balldirect * (float)i);
				}
				Vector3 localPosition = balls[i].localPosition;
				if (localPosition.x <= -360f)
				{
					balls[i - 1].gameObject.SetActive(value: false);
					balls[i].gameObject.SetActive(value: false);
					if (!flag)
					{
						bigBallGO.gameObject.SetActive(value: true);
						float num2 = formPos.y + direction.y * (-360f - formPos.x) / direction.x;
						float num3 = num2 * 9f / 360f;
						v = new Vector2(-351f, num2 - num3);
						num = i;
						flag = true;
						vector = new Vector2(formPos.x + 360f - 9f, formPos.y + num2 - num3).normalized;
						bigBallGO.localPosition = v;
					}
					continue;
				}
				Vector3 localPosition2 = balls[i].localPosition;
				if (localPosition2.x >= 360f)
				{
					balls[i - 1].gameObject.SetActive(value: false);
					balls[i].gameObject.SetActive(value: false);
					if (!flag)
					{
						bigBallGO.gameObject.SetActive(value: true);
						float num4 = formPos.y + direction.y * (360f - formPos.x) / direction.x;
						float num5 = num4 * 9f / 360f;
						v = new Vector2(351f, num4 - num5);
						num = i;
						flag = true;
						vector = new Vector2(formPos.x - 360f + 9f, formPos.y + num4 - num5).normalized;
						bigBallGO.localPosition = v;
					}
					continue;
				}
				Vector3 localPosition3 = balls[i].localPosition;
				float y = localPosition3.y;
				Vector3 localPosition4 = toppos.localPosition;
				if (y >= localPosition4.y)
				{
					balls[i - 1].gameObject.SetActive(value: false);
					balls[i].gameObject.SetActive(value: false);
					if (!flag)
					{
						bigBallGO.gameObject.SetActive(value: true);
						float x = direction.x;
						Vector3 localPosition5 = toppos.localPosition;
						float num6 = x * (localPosition5.y - formPos.y) / direction.y + formPos.x;
						float x2 = num6;
						Vector3 localPosition6 = toppos.localPosition;
						v = new Vector2(x2, localPosition6.y);
						num = i;
						flag = true;
						vector = new Vector2(v.x - formPos.x, formPos.y - v.y).normalized;
						bigBallGO.localPosition = v;
					}
				}
				else
				{
					Vector3 localPosition7 = balls[i].localPosition;
					float y2 = localPosition7.y;
					Vector3 localPosition8 = bottompos.localPosition;
					if (y2 <= localPosition8.y)
					{
						balls[i].gameObject.SetActive(value: false);
					}
					else
					{
						balls[i].gameObject.SetActive(value: true);
					}
				}
			}
		}

		public void HideAll()
		{
			for (int i = 0; i < balls.Length; i++)
			{
				balls[i].gameObject.SetActive(value: false);
			}
			if ((bool)bigBallGO)
			{
				bigBallGO.gameObject.SetActive(value: false);
			}
		}
	}
}

